package com.test.websample.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {
    private static final String KEY = "key";

    public static String genJwt(Map<String, Object> args) {
        return JWT.create().withClaim("claims", args).
                withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24)).
                sign(Algorithm.HMAC256(KEY));
    }

    public static Map<String, Object> verifyAndGetJwtInfo(String token) {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(KEY)).build();
        verifier.verify(token);
        return verifier.verify(token).getClaim("claims").asMap();
    }
}
